﻿using System;
using Thrift.Protocol;
using Thrift.Transport;
using ThriftContract;

namespace ThriftClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // StartThriftClientSingle();
            StartThriftClientToServerS();
            Console.ReadKey();
        }

        /// <summary>
        /// 单服务
        /// </summary>
        public static void StartThriftClientSingle()
        {
            using (TTransport transport = new TSocket("localhost", 8800))
            using (TProtocol protocol = new TBinaryProtocol(transport))
            using (var clientUser = new UserService.Client(protocol))
            {
                transport.Open();
                User u = clientUser.Get(1);
                Console.WriteLine($"{u.Id},{u.Name}");
                Console.WriteLine("调用GetAll");
                foreach (var user in clientUser.GetAll())
                {
                    Console.WriteLine($"{user.Id},{user.Name}");
                }

                Console.WriteLine("调用Save");
               SaveResult result= clientUser.Save(u);
                Console.WriteLine(result.ToString());


            }
        }

        /// <summary>
        /// 多服务
        /// </summary>
        public static void StartThriftClientToServerS()
        {
            using (TTransport transport = new TSocket("localhost", 8800))
            using (TProtocol protocol = new TBinaryProtocol(transport))
            using (var protocolUserService = new TMultiplexedProtocol(protocol,
                "userService"))
            using (var clientUser = new UserService.Client(protocolUserService))
            using (var protocolCalcService = new TMultiplexedProtocol(protocol,
                "calcService"))
            using (var clientCalc = new CalcService.Client(protocolCalcService))
            {
                transport.Open();
                User u = clientUser.Get(1);
                Console.WriteLine($"{u.Id},{u.Name}");
                Console.WriteLine(clientCalc.Add(1, 2));
            }

        }
    }
}
